import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-02-02
 * Time: 14:41
 * <p>
 * https://www.nowcoder.com/share/jump/7745334641706856077277
 */
public class BM92 {
    public int maxLength(int[] arr) {
        if (arr == null || arr.length == 0) {
            return 0;
        }
        // Set 记录不相同的元素
        Set<Integer> set = new HashSet<>();
        set.add(arr[0]);
        // 记录子数组的边界
        int left = 0, right = 0;
        // 最大长度
        int maxLen = 1;
        for (right = 1; right < arr.length; right++) {
            if (!set.contains(arr[right])) {
                set.add(arr[right]);
                maxLen = Math.max(maxLen, right - left + 1);
            } else {
                // 开始滑动
                while (set.contains(arr[right])) {
                    set.remove(arr[left++]);
                }
                right--;
            }
        }
        return maxLen;
    }
}
